//
//  DoubleTwoColorProgressBar.m
//  SKPAD
//
//  Created by 何睿 on 15/6/23.
//  Copyright (c) 2015年 heeeruiii. All rights reserved.
//

#import "DoubleTwoColorProgressBar.h"

@implementation DoubleTwoColorProgressBar

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //初始化两个背景图
        UIImage * image = [UIImage imageNamed:@"trough(2).png"];
        _backgroundView = [[UIImageView alloc] initWithImage:image];
        _backgroundView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView.backgroundColor = [UIColor clearColor];
        UIImage * image2 = [UIImage imageNamed:@"trough(2)_grey.png"];
        _backgroundView2 = [[UIImageView alloc] initWithImage:image2];
        _backgroundView2.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView2.backgroundColor = [UIColor clearColor];
        
        _maxLeftValue = 100.0;
        _minLeftValue = 0.0;
        _realLeftValue = 0.0;
        
        _maxRightValue = 100.0;
        _minRightValue = 0.0;
        _realRightValue = 0.0;
        //画刻度层
        [self paintingScale];
        [self initRealValueTrack];
        [self.layer insertSublayer:_backgroundView2.layer atIndex:0];
        [self.layer insertSublayer:_backgroundView.layer atIndex:3];
        [self.layer insertSublayer:_layerScale atIndex:1];
        [self.layer insertSublayer:_layerLeftReal atIndex:2];
        [self.layer insertSublayer:_layerRightReal atIndex:2];
    }
    return  self;
}

-(void) paintingScale
{
    //初始化图层
    _layerScale = [CALayer layer];
    _layerScale.frame = self.bounds;
    _layerScale.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    //初始化刻度层
    _trackLayerScale = [CAShapeLayer layer];
    _trackLayerScale.frame = self.bounds;
    _trackLayerScale.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    _trackLayerScale.fillColor = [UIColor clearColor].CGColor;
    _trackLayerScale.strokeColor = [[UIColor blueColor] CGColor];
    _trackLayerScale.lineCap = kCALineCapRound;
    
    //初始化颜色层
    //绿色
    _gradientLayerGreen = [CAGradientLayer layer];
    _gradientLayerGreen.frame = CGRectMake(0.0, self.bounds.size.height * 0.3184, self.bounds.size.width, self.bounds.size.height * 0.6816);
    //图层中心，根据整个空间坐标系定位
    _gradientLayerGreen.position = CGPointMake(CGRectGetWidth(self.bounds)/2.0, self.bounds.size.height * 0.3184 + self.bounds.size.height * 0.6816 / 2.0);
    //黄色
    _gradientLayerYellow = [CAGradientLayer layer];
    _gradientLayerYellow.frame = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height * 0.3184);
    _gradientLayerYellow.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, self.bounds.size.height * 0.3184 / 2.0);
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                      animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 0.25f;
    animation.keyTimes = @[@(0), @(0.5), @(1.0)];
    animation.timingFunction = [CAMediaTimingFunction
                                functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.trackLayerScale addAnimation:animation forKey:nil];
    
    UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
    CGFloat eachValue = self.frame.size.height / 130;
    _trackLayerScale.lineWidth = eachValue;
    CGFloat startY = self.frame.size.height - eachValue * 3;
    for (int i = 0; i < 42; ++i)
    {
        [lineTrajectory moveToPoint:CGPointMake(0, startY)];
        [lineTrajectory addLineToPoint:CGPointMake(_trackLayerScale.frame.size.width, startY)];
        startY -= eachValue * 3;
    }
    
    //连线
    _trackLayerScale.path = lineTrajectory.CGPath;
    _trackLayerScale.strokeStart = 0.f;
    _trackLayerScale.strokeEnd = 1.f;
    UIColor * darkGreen = (id)[[UIColor colorWithRed:38.0/255 green:90.0/255 blue:43.0/255 alpha:1] CGColor];
    UIColor * darkYellow = (id)[[UIColor colorWithRed:86.0/255 green:88.0/255 blue:42.0/255 alpha:1] CGColor];
    
    [_gradientLayerGreen setColors:[NSArray arrayWithObjects:darkGreen,darkGreen, nil]];
    [_gradientLayerGreen setLocations:@[@0.0,@1.0]];
    [_gradientLayerGreen setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerGreen setEndPoint:CGPointMake(0.5, 0)];
    
    [_gradientLayerYellow setColors:[NSArray arrayWithObjects:darkYellow,darkYellow, nil]];
    [_gradientLayerYellow setLocations:@[@0.0,@1.0]];
    [_gradientLayerYellow setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerYellow setEndPoint:CGPointMake(0.5, 0)];
    
    [_layerScale addSublayer:_gradientLayerGreen];
    [_layerScale addSublayer:_gradientLayerYellow];
    [_layerScale setMask:_trackLayerScale];
}

- (void) initRealValueTrack
{
    //初始化颜色层
    UIColor * greenColor = (id)[[UIColor greenColor] CGColor];
    UIColor * yellowColor = (id)[[UIColor yellowColor] CGColor];
    
    //初始化左侧值图层
    _layerLeftReal = [CALayer layer];
    _layerLeftReal.frame = CGRectMake(0.0, 0.0, self.bounds.size.width / 2.0, self.bounds.size.height);
    _layerLeftReal.position = CGPointMake(CGRectGetWidth(self.frame)/4.0, CGRectGetHeight(self.frame)/2.0);
    //初始化左侧刻度层
    _trackLayerLeftReal = [CAShapeLayer layer];
    _trackLayerLeftReal.frame = CGRectMake(0.0, 0.0, _layerLeftReal.bounds.size.width , _layerLeftReal.bounds.size.height);
    _trackLayerLeftReal.position = CGPointMake(CGRectGetWidth(self.frame)/4.0, CGRectGetHeight(self.frame)/2.0);
    _trackLayerLeftReal.fillColor = [UIColor clearColor].CGColor;
    _trackLayerLeftReal.strokeColor = [[UIColor blueColor] CGColor];
    _trackLayerLeftReal.strokeStart = 0.f;
    _trackLayerLeftReal.strokeEnd = 1.f;
    
    //绿色
    _realLeftGreen = [CAGradientLayer layer];
    _realLeftGreen.frame = CGRectMake(_gradientLayerGreen.bounds.origin.x, _gradientLayerGreen.bounds.origin.y, _layerLeftReal.bounds.size.width, _gradientLayerGreen.bounds.size.height);
    _realLeftGreen.position = CGPointMake(_layerLeftReal.bounds.size.width / 2, _gradientLayerGreen.position.y);
    [_realLeftGreen setColors:[NSArray arrayWithObjects:greenColor, greenColor, nil]];
    [_realLeftGreen setLocations:@[@0.0,@1.0]];
    [_realLeftGreen setStartPoint:CGPointMake(0.5, 1)];
    [_realLeftGreen setEndPoint:CGPointMake(0.5, 0)];
    //图层中心，根据整个空间坐标系定位
    
    //黄色
    _realLeftYellow = [CAGradientLayer layer];
    _realLeftYellow.frame = CGRectMake(_gradientLayerYellow.bounds.origin.x, _gradientLayerYellow.bounds.origin.y, _layerLeftReal.bounds.size.width, _gradientLayerYellow.bounds.size.height);
    _realLeftYellow.position = CGPointMake(_layerLeftReal.bounds.size.width / 2, _gradientLayerYellow.position.y);
    [_realLeftYellow setColors:[NSArray arrayWithObjects:yellowColor, yellowColor, nil]];
    [_realLeftYellow setLocations:@[@0.0,@1.0]];
    [_realLeftYellow setStartPoint:CGPointMake(0.5, 1)];
    [_realLeftYellow setEndPoint:CGPointMake(0.5, 0)];

//--------------------------------------
    //初始化右侧值图层
    _layerRightReal = [CALayer layer];
    _layerRightReal.frame = CGRectMake(self.bounds.size.width / 2.0, 0.0, self.bounds.size.width / 2.0, self.bounds.size.height);
    _layerRightReal.position = CGPointMake(CGRectGetWidth(self.frame)/4.0 * 3, CGRectGetHeight(self.frame)/2.0);
    //初始化右侧刻度层
    _trackLayerRightReal = [CAShapeLayer layer];
    _trackLayerRightReal.frame = CGRectMake(0.0, 0.0, _layerRightReal.bounds.size.width, _layerRightReal.bounds.size.height);
    _trackLayerRightReal.position = CGPointMake(CGRectGetWidth(self.frame)/4.0, CGRectGetHeight(self.frame)/2.0);
    _trackLayerRightReal.fillColor = [UIColor clearColor].CGColor;
    _trackLayerRightReal.strokeColor = [[UIColor blueColor] CGColor];
    _trackLayerRightReal.strokeStart = 0.f;
    _trackLayerRightReal.strokeEnd = 1.f;
    
    //绿色
    _realRightGreen = [CAGradientLayer layer];
    _realRightGreen.frame = CGRectMake(_gradientLayerGreen.bounds.origin.x, _gradientLayerGreen.bounds.origin.y, _layerRightReal.bounds.size.width, _gradientLayerGreen.bounds.size.height);
    _realRightGreen.position = CGPointMake(_layerRightReal.bounds.size.width / 2, _gradientLayerGreen.position.y);
    [_realRightGreen setColors:[NSArray arrayWithObjects:greenColor, greenColor, nil]];
    [_realRightGreen setLocations:@[@0.0,@1.0]];
    [_realRightGreen setStartPoint:CGPointMake(0.5, 1)];
    [_realRightGreen setEndPoint:CGPointMake(0.5, 0)];
    //图层中心，根据整个空间坐标系定位
    
    //黄色
    _realRightYellow = [CAGradientLayer layer];
    _realRightYellow.frame = CGRectMake(_gradientLayerYellow.bounds.origin.x, _gradientLayerYellow.bounds.origin.y, _layerRightReal.bounds.size.width, _gradientLayerYellow.bounds.size.height);
    _realRightYellow.position = CGPointMake(_layerRightReal.bounds.size.width / 2, _gradientLayerYellow.position.y);

    [_realRightYellow setColors:[NSArray arrayWithObjects:yellowColor, yellowColor, nil]];
    [_realRightYellow setLocations:@[@0.0,@1.0]];
    [_realRightYellow setStartPoint:CGPointMake(0.5, 1)];
    [_realRightYellow setEndPoint:CGPointMake(0.5, 0)];
    
    [_layerLeftReal addSublayer:_realLeftGreen];
    [_layerLeftReal addSublayer:_realLeftYellow];
    [_layerLeftReal setMask:_trackLayerLeftReal];
    [_layerRightReal addSublayer:_realRightGreen];
    [_layerRightReal addSublayer:_realRightYellow];
    [_layerRightReal setMask:_trackLayerRightReal];
}

- (void)setRealLeftValue:(CGFloat)realLeftValue;
{
    if (_realLeftValue != realLeftValue)
    {
        NSLog(@"LeftValue = %f", realLeftValue);
        _realLeftValue = realLeftValue;
        int tempValue = (int)((realLeftValue - _minLeftValue)/(_maxLeftValue - _minLeftValue) * 42);
        UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
        CGFloat eachValue = self.frame.size.height / 130;
        _trackLayerLeftReal.lineWidth = eachValue;
        CGFloat startY = self.frame.size.height - eachValue * 3;
        for (int i = 0; i < tempValue; ++i)
        {
            [lineTrajectory moveToPoint:CGPointMake(0.0, startY)];
            [lineTrajectory addLineToPoint:CGPointMake(self.bounds.size.width / 2.0, startY)];
            startY -= eachValue * 3;
        }
        //连线
        _trackLayerLeftReal.path = lineTrajectory.CGPath;
    }
}

- (void)setRealRightValue:(CGFloat)realRightValue;
{
    
    if (_realRightValue != realRightValue)
    {
        NSLog(@"RightValue = %f", realRightValue);
        _realRightValue = realRightValue;
        int tempValue = (int)((realRightValue - _minRightValue)/(_maxRightValue - _minRightValue) * 42);
        UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
        CGFloat eachValue = self.frame.size.height / 130;
        _trackLayerRightReal.lineWidth = eachValue;
        CGFloat startY = self.frame.size.height - eachValue * 3;
        for (int i = 0; i < tempValue; ++i)
        {
            [lineTrajectory moveToPoint:CGPointMake(0, startY)];
            [lineTrajectory addLineToPoint:CGPointMake(self.bounds.size.width / 2.0, startY)];
            startY -= eachValue * 3;
        }
        //连线
        _trackLayerRightReal.path = lineTrajectory.CGPath;
    }
}

@end
